<?php

class Dosen extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('parser');
        $this->load->model('uwd');
        $login = $this->session->userdata('login');
        $privilege = $this->session->userdata('pri');
        if ($login != TRUE || $privilege != 2) {
            redirect('main/logout');
        }
    }

    function index() {
        redirect('dosen/home');
    }

    function home() {
        $id = $this->session->userdata('username');
        $da = $this->uwd->nama($id);
        if (!empty($da[0]->nama)) {
            $data['nama'] = $da[0]->nama; //erorr,cek
        } else {
            $data['nama'] = array();
        }
        $data['page'] = 'Home Dosen | Universitas Widya Dharma';
        $this->parser->parse('themes/header', $data);
        $this->load->view('dosen/nav');
        $this->parser->parse('themes/content', $data);
        $this->load->view('themes/footer');
    }

    function profil() {
        $usr = $this->session->userdata('username');
        $user = $this->uwd->profile($usr);
        $data['nik'] = $user[0]->username;
        $data['nama'] = $user[0]->nama;
        $data['rumah'] = $user[0]->rumah;
        $data['kantor'] = $user[0]->kantor;
        $data['email'] = $user[0]->email;
        $data['email_al'] = $user[0]->email_al;
        $data['phone'] = $user[0]->telp;
        $data['blog'] = $user[0]->blog;
        $data['sino'] = $user[0]->sinopsis;
        $data['page'] = 'Home Dosen | Universitas Widya Dharma';
        $data['username'] = $usr;
        $this->parser->parse('themes/header', $data);
        $this->load->view('dosen/nav');
        $this->parser->parse('dosen/profil', $data);
        $this->load->view('themes/footer');
    }

    function profil_update() {
        $id = $this->session->userdata('username');
        $data = array(
            'nama' => $this->input->post('nama'),
            'rumah' => $this->input->post('rumah'),
            'kantor' => $this->input->post('kantor'),
            'telp' => $this->input->post('phone'),
            'email' => $this->input->post('email'),
            'email_al' => $this->input->post('email_al'),
            'blog' => $this->input->post('blog'),
            'sinopsis' => $this->input->post('sino')
        );
        $this->uwd->profile_upd($id, $data);
        $data['page'] = 'Update Profil Dosen | Universitas Widya Dharma';
        $data['username'] = $id;
        $data['alert'] = '<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">×</button><strong>Update Profil berhasil.</strong></div>';
        $this->parser->parse('themes/header', $data);
        $this->load->view('dosen/nav');
        $this->parser->parse('themes/alert', $data);
        $this->load->view('themes/footer');
    }

    function password() {
        $data['page'] = 'Ganti Password | Universitas Widya Dharma';
        $id = $this->session->userdata('username');
        $da = $this->uwd->nama($id);
        $data['nama'] = $da[0]->nama;
        $this->parser->parse('themes/header', $data);
        $this->load->view('dosen/nav');
        $this->parser->parse('dosen/password', $data);
        $this->load->view('themes/footer');
    }

    function password_update() {
        $lama = $this->input->post('lama');
        $id = $this->session->userdata('username');
        $cek = $this->uwd->passlama($id, $lama);
        if ($cek == TRUE) {
            $pass = array(
                'password' => $this->input->post('baru')
            );
            $this->uwd->passbaru($id, $pass);
            $da = $this->uwd->nama($id);
            $data['nama'] = $da[0]->nama;
            $data['page'] = 'Update Profil Dosen | Universitas Widya Dharma';
            $data['username'] = $id;
            $data['alert'] = '<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">×</button><strong>Password berhasil dirubah.</strong></div>';
            $this->parser->parse('themes/header', $data);
            $this->load->view('dosen/nav');
            $this->parser->parse('themes/alert', $data);
            $this->load->view('themes/footer');
        } else {
            $data['page'] = 'Update Profil Dosen | Universitas Widya Dharma';
            $data['username'] = $id;
            $da = $this->uwd->nama($id);
            $data['nama'] = $da[0]->nama;
            $data['alert'] = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button><strong>Password lama tidak cocok.</strong></div>';
            $this->parser->parse('themes/header', $data);
            $this->load->view('dosen/nav');
            $this->parser->parse('themes/alert', $data);
            $this->load->view('themes/footer');
        }
    }

    function upload() {
        $data['page'] = 'Upload Files | Universitas Widya Dharma';
        $id = $this->session->userdata('username');
        $da = $this->uwd->nama($id);
        $data['nama'] = $da[0]->nama;
        if ($this->uwd->matkul()) {
            $data['matkul'] = $this->uwd->matkul();
        } else {
            $data['matkul'] = array();
        }
        $this->parser->parse('themes/header', $data);
        $this->load->view('dosen/nav');
        $this->parser->parse('dosen/upload', $data);
        $this->load->view('themes/footer');
    }

    function upload_file() {
        $this->load->library('form_validation', 'encrypt');
        $this->form_validation->set_rules('judul', 'Username', 'required');
        $this->form_validation->set_rules('sinopsis', 'Password', 'required');
        if ($this->form_validation->run() == FALSE) {
            $data['page'] = 'Upload Files | Universitas Widya Dharma';
            $id = $this->session->userdata('username');
            $da = $this->uwd->nama($id);
            $data['nama'] = $da[0]->nama;
            if ($this->uwd->matkul()) {
                $data['matkul'] = $this->uwd->matkul();
            } else {
                $data['matkul'] = array();
            }
            $data['alert'] = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">×</button><strong>Semua fields harus diisi.</strong></div>';
            $this->parser->parse('themes/header', $data);
            $this->load->view('dosen/nav');
            $this->parser->parse('themes/alert', $data);
            $this->parser->parse('dosen/upload', $data);
            $this->load->view('themes/footer');
        } else {
            $config['upload_path'] = './files/';
            $config['allowed_types'] = 'doc|docx|txt|pdf|xls|xlsx|ppt|pptx';
            $config['max_size'] = '10240';
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload()) {
                echo $this->upload->display_errors();
            } else {
                $username = $this->session->userdata('username');
                $matkul = $this->input->post('matkul');
                $status = $this->input->post('status');
                $judul = $this->input->post('judul');
                $sinopsis = $this->input->post('sinopsis');
                $file = $this->upload->data();
                $cook = $this->input->request_headers();
                $input = array(
                    'hash' => $this->encrypt->sha1($file['file_name'] . time() . $this->session->userdata('username') . $this->input->ip_address()) . $cook['Cookie'],
                    'file_name' => $file['file_name'],
                    'file_type' => $file['file_type'],
                    'file_path' => $file['file_path'],
                    'full_path' => base_url() . 'files/' . $file['file_name'],
                    'raw_name' => $file['raw_name'],
                    'orig_name' => $file['orig_name'],
                    'client_name' => $file['client_name'],
                    'file_ext' => $file['file_ext'],
                    'file_size' => $file['file_size'],
                    'matkul_id' => $matkul,
                    'judul' => $judul,
                    'sinopsis' => $sinopsis,
                    'status' => $status,
                    'dosen_id' => $username
                );
                $this->uwd->upload($input);
                redirect('dosen/upload_success');
            }
        }
    }

    function upload_success() {
        $data['page'] = 'Upload Success | Universitas Widya Dharma';
        $id = $this->session->userdata('username');
        $da = $this->uwd->nama($id);
        $data['nama'] = $da[0]->nama;
        if ($this->uwd->matkul()) {
            $data['matkul'] = $this->uwd->matkul();
        } else {
            $data['matkul'] = array();
        }
        $data['alert'] = '<div class="alert alert-info"><strong>Upload materi berhasil.</strong></div>';
        $this->parser->parse('themes/header', $data);
        $this->load->view('dosen/nav');
        $this->parser->parse('themes/alert', $data);
        $this->parser->parse('dosen/upload', $data);
        $this->load->view('themes/footer');
    }

    function materi() {
        $this->load->library('pagination');
        $this->load->helper('download');
        $data['page'] = 'Daftar Materi | Universitas Widya Dharma';
        $id = $this->session->userdata('username');
        $da = $this->uwd->nama($id);
        $data['nama'] = $da[0]->nama;
        $config['base_url'] = $this->config->config['base_url'] . '/index.php/dosen/materi/';
        $config['total_rows'] = $this->uwd->count_materi($id);
        $config['per_page'] = 5;
        $config['num_links'] = 2;
        $config['uri'] = $this->uri->segment(3);
        $this->pagination->initialize($config);
        if ($this->uwd->materi($id, $config)) {
            $data['materi'] = $this->uwd->materi($id, $config);
        } else {
            $data['materi'] = array();
        }
        $this->parser->parse('themes/header', $data);
        $this->load->view('dosen/nav');
        $this->parser->parse('dosen/materi', $data);
        $this->load->view('themes/footer');
    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>
